﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace DecoratedCommandSpike
{
    class Command
    {
        public string Name { get; set; }

        public virtual Action Execute { get; set; }
    }

    class DecoratedCommand : Command
    {
        public Func<bool> PreExecute { get; set; }

        public override Action Execute
        {
            get
            {
                Action result = () => { if (this.PreExecute()) base.Execute(); };
                return result;
            }
            set { base.Execute = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var commands = new List<Command>();
            //commands.Add(new Command { Name = "Print", Execute = () => Console.WriteLine("I'm Print!") });
            //commands.Add(new Command { Name = "Save", Execute = () => Console.WriteLine("Hello from print!") });
            commands.Add(new DecoratedCommand
            {
                Name = "DecoratedSave",
                Execute = () => Console.WriteLine("Hello from DecoratedSave!"),
                PreExecute = () => { Console.WriteLine("Hello from DecoratedSave Preexecute!"); return true; }
            });
            foreach (var cmd in commands)
            {
                cmd.Execute();
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
